package com.smart.reader.controller;

import com.smart.reader.common.Page;
import com.smart.reader.common.R;
import com.smart.reader.enums.SuccessResultCode;
import com.smart.reader.exception.CustomException;
import com.smart.reader.model.bo.StudentInfoAddBo;
import com.smart.reader.model.bo.StudentInfoUpdateBo;
import com.smart.reader.model.vo.StudentInfoVo;
import com.smart.reader.service.StudentInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@RequestMapping("/api/v1/student/info")
@RestController
@Validated
@Api(tags = "学生管理", description = "学生管理相关接口")
public class StudentInfoController {
    @Resource
    private StudentInfoService studentInfoService;
    
    /**
     * 新增数据
     *
     * @param studentInfo 实例对象
     * @return 实例对象
     */
    @ApiOperation("新增学生数据")
    @PostMapping("/add")
    public R add(@RequestBody @Valid StudentInfoAddBo studentInfo) throws CustomException {
        studentInfoService.add(studentInfo);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg());
    }
    
    @ApiOperation("删除学生数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生ID", required = true)
    })
    @DeleteMapping("/delete/{id}")
    public R delete(@NotNull(message = "学生ID不能为空") @PathVariable("id") Long id) throws CustomException {
        studentInfoService.delete(id);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg());
    }
    
    @ApiOperation("修改学生数据")
    @PostMapping("/edit")
    public R edit(@RequestBody @Valid StudentInfoUpdateBo studentInfo) throws CustomException {
        studentInfoService.edit(studentInfo);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg());
    }
    
    @GetMapping("queryInfo")
    @ApiOperation("获取学生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true, dataType = "int", defaultValue = "10"),
    })
    public R<Page<StudentInfoVo>> queryInfo(int pageNum, int pageSize) {
        Page<StudentInfoVo> result = studentInfoService.queryInfo(pageNum, pageSize);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg(), result);
    }
    
    @ApiOperation("根据学生ID查询学生详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生ID", required = true)
    })
    @GetMapping("/detail/{id}")
    public R<StudentInfoVo> detail(@NotNull(message = "学生ID不能为空") @PathVariable("id") Long id) {
        StudentInfoVo studentInfoVo = studentInfoService.detail(id);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg(), studentInfoVo);
    }
}
